package com.example.demo.service;


import cn.hutool.core.util.RandomUtil;
import com.example.demo.dto.OrderDto;
import com.example.demo.entity.OrderItem;
import com.example.demo.dto.OrderItemDto;
import com.example.demo.dto.OrderShoppingDto;
import com.example.demo.entity.Orders;
import com.example.demo.entity.ProductSku;
import com.example.demo.entity.Shopping;
import com.example.demo.mapper.OrdersMapper;
import com.example.demo.model.Result;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional(rollbackFor = Exception.class)
public class OrdersService {
    private OrdersMapper ordersMapper;

    public OrdersService(OrdersMapper ordersMapper) {
        this.ordersMapper = ordersMapper;
    }

//    public Result getOrdersById(Integer id){
//        return Result.buildSuccess(ordersMapper.selectOrdersById(id));
//    }

    public Result getOrdersByStatus(OrderDto orderDto){
        return Result.buildSuccess(ordersMapper.selectOrdersByStatus(orderDto));
    }

    //每三秒钟扫一次订单状态为1的订单看超时没有
    @Scheduled(cron = "0/3 * * * * ?")
    public Result getAllOrder(){
        System.out.println("1212");
        ordersMapper.selectAllOrder().forEach(e -> {
            long currentTime=System.currentTimeMillis();
            long time = e.getCreateTime().getTime();
            if(currentTime-time > 15*60*1000){//表示超过15分钟
                //修改状态
                ordersMapper.updataOrderStatus(e.getOrderId());
                //添加库存
                List<OrderItem> list = e.getOrderItem();
                list.forEach(e1 -> {
                    ordersMapper.cancelOrder(e1.getSkuId(), e1.getBuyCounts());
                });
            }
//            System.out.println(e);
        });
        return Result.buildSuccess(ordersMapper.selectAllOrder());
    }


    public void cancalOrderUpdateInfo(OrderDto orderDto) {
        //修改用户的状态
        ordersMapper.cancelOrderUpdateStatus(orderDto.getOrderId());

        //添加库存
        List<OrderItem> list = orderDto.getOrderItem();
        list.forEach(e -> {
            ordersMapper.cancelOrder(e.getSkuId(), e.getBuyCounts());
        });
    }

    public void addOrderItem(OrderItemDto orderItemDto){
        ordersMapper.addOrder(orderItemDto);
        for (int i = 0 ; i < orderItemDto.getShopping().size();i++){
            List<Shopping> shopping = orderItemDto.getShopping();
            OrderShoppingDto orderShoppingDto = new OrderShoppingDto();
            orderShoppingDto.setItemId(RandomUtil.randomNumbers(18));
            orderShoppingDto.setOrderId(orderItemDto.getOrderId());
            orderShoppingDto.setProductId(shopping.get(i).getProductId());
            orderShoppingDto.setProductName(shopping.get(i).getSkuProps());
            orderShoppingDto.setProductImg(shopping.get(i).getProductSkus().getSkuImg());
            orderShoppingDto.setSkuId(shopping.get(i).getSkuId());
            orderShoppingDto.setSkuName(shopping.get(i).getSkuProps());
            orderShoppingDto.setPrice(shopping.get(i).getProductSkus().getSellPrice());
            orderShoppingDto.setTotalPrice(shopping.get(i).getCartNum() * shopping.get(i).getProductSkus().getSellPrice());
            orderShoppingDto.setCounts(shopping.get(i).getCartNum());
            ordersMapper.addOrderItem(orderShoppingDto);

            ProductSku productSku = new ProductSku();
            productSku.setSkuId(shopping.get(i).getSkuId());
            productSku.setStock(shopping.get(i).getProductSkus().getStock() - shopping.get(i).getCartNum());
            ordersMapper.updateStock(productSku);
        }
    }

    public Orders findOne(OrderItemDto orderItemDto) {
        return ordersMapper.selectOneOrder(orderItemDto.getOrderId());
    }

    }

